package com.mall.ware.listener;

import com.mall.common.dto.OrderDTO;
import com.mall.common.dto.OrderNoteDto;
import com.mall.common.dto.WareOrderTaskDetailDTO;
import com.mall.common.enume.OrderStatusEnum;
import com.mall.common.utils.ConvertUtils;
import com.mall.common.utils.Result;
import com.mall.ware.entity.WareOrderTaskDetailEntity;
import com.mall.ware.entity.WareOrderTaskEntity;
import com.mall.ware.feign.OrderFeignClient;
import com.mall.ware.service.WareOrderTaskDetailService;
import com.mall.ware.service.WareOrderTaskService;
import com.mall.ware.service.WareSkuService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Objects;


/**
 * @author: 谢君臣
 * @Date: 2022/8/11-16:39
 * @version: 1.0
 * @function:
 */
@Slf4j
@RabbitListener(queues = "stock.release.queue")
@Component
public class ReleaseStockListener {

    @Resource
    private WareOrderTaskDetailService wareOrderTaskDetailService;
    @Autowired
    private OrderFeignClient orderFeignClient;
    @Resource
    private WareSkuService wareSkuService;
    @Resource
    private WareOrderTaskService orderTaskService;

    @RabbitHandler
    public void releaseLockStock(WareOrderTaskDetailDTO detailDTO, Message message, Channel channel) throws IOException {
        log.info("收到库存锁定成功消息");
        // 验锁库存单
        try {
            unLockStock(detailDTO);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (RuntimeException e) {
           log.error(e.getMessage());
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

    @RabbitHandler
    public void releaseLockStock(OrderNoteDto orderDTO, Message message, Channel channel) throws IOException {
        log.info("收到订单关闭，解锁库存消息");
        // 验锁库存单
        try {
            unLockStock(orderDTO.getOrderSn());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (RuntimeException e) {
            log.info(e.getMessage());
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

    @Transactional
    public void unLockStock(String orderSn) throws RuntimeException {
        WareOrderTaskEntity wareOrderTask = orderTaskService.getOrderTaskByOrderSn(orderSn);
        List<WareOrderTaskDetailEntity> listDetail = wareOrderTaskDetailService.listByTaskId(wareOrderTask.getId());
        for (WareOrderTaskDetailEntity detailEntity : listDetail) {
            wareSkuService.unLockStock(detailEntity.getId(), detailEntity.getSkuId(), detailEntity.getWareId(), detailEntity.getSkuNum());
        }
    }


    public void unLockStock(WareOrderTaskDetailDTO detailDTO) throws RuntimeException {

        WareOrderTaskDetailEntity detailEntity = wareOrderTaskDetailService.selectById(detailDTO.getId());
        if (detailEntity!=null&&detailEntity.getLockStatus()==1){
            //查单
            Result<Object> result = orderFeignClient.getByOrderSn(detailDTO.getOrderSn());
            if (result.getCode()==0){
                OrderDTO data = ConvertUtils.sourceToTarget(result.getData(), OrderDTO.class);
                if ( data == null || Objects.equals(OrderStatusEnum.CANCLED.getCode(), data.getStatus())) {
                    // 解锁库存
                    wareSkuService.unLockStock(detailDTO.getId(), detailDTO.getSkuId(), detailDTO.getWareId(), detailDTO.getSkuNum());
                }
            }else
                throw new RuntimeException("远程查单异常");
        }
    }
}
